﻿//-----------------------------------------------------------------------------
// FILE:	    SdkCatalogItem.cs
// CONTRIBUTOR: Jeff Lill
// COPYRIGHT:   Copyright (c) 2021 by neonFORGE, LLC.  All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;

using Neon.Common;

using Newtonsoft.Json;

namespace RaspberryDebugger
{
    /// <summary>
    /// Describes an .NET Core SDK download.
    /// </summary>
    internal class SdkCatalogItem
    {
        private bool?   isStandAlone;

        /// <summary>
        /// The SDK name (like "3.1.402").
        /// </summary>
        [JsonProperty(PropertyName = "Name", Required = Required.Always)]
        public string Name { get; set; }

        /// <summary>
        /// SDK版本(如“3.1.8”)。
        /// </summary>
        [JsonProperty(PropertyName = "Version", Required = Required.Always)]
        public string Version { get; set; }

        /// <summary>
        /// SDK 32位或64位版本。  
        /// </summary>
        [JsonProperty(PropertyName = "Architecture", Required = Required.Always)]
        public SdkArchitecture Architecture { get; set; }

        /// <summary>
        /// 二进制下载的URL。
        /// </summary>
        [JsonProperty(PropertyName = "Link", Required = Required.Always)]
        public string Link { get; set; }

        /// <summary>
        ///  SHA512哈希预期下载  
        /// </summary>
        [JsonProperty(PropertyName = "SHA512", Required = Required.Always)]
        public string SHA512 { get; set; }

        /// <summary>
        /// 指示SDK是否实际可用。 这个默认值  
        /// <c>false</c>。 这是为一些早期的.net 5.0版本设置的
        /// that didn't work on ARM64.
        /// </summary>
        [JsonProperty(PropertyName = "Unusable", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        [DefaultValue(false)]
        public bool IsUnusable { get; set; }

        /// <summary>
        /// 表示SDK可用。
        /// </summary>
        [JsonIgnore]
        public bool IsUsable => !IsUnusable;

        /// <summary>
        /// 表明这是一个独立的SDK，而不是集成到Visual Studio中的SDK;  
        /// </summary>
        [JsonIgnore]
        public bool IsStandalone
        {
            get
            {
                if (isStandAlone.HasValue)
                {
                    return isStandAlone.Value;
                }

                // Standalone SDKs seem to have name patch versions < 200.

                isStandAlone = SemanticVersion.Parse(Name).Patch < 200;

                return isStandAlone.Value;
            }
        }
    }
}
